/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.smscenter.biz.entity.SensitiveWords;
import com.sanerzone.smscenter.biz.iface.CachRefreshInterface;
import com.sanerzone.smscenter.modules.sms.entity.SmsSensitiveWords;
import com.sanerzone.smscenter.modules.sms.service.SmsSensitiveWordsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 敏感词管理Controller
 * @author jianr.z
 * @version 2018-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsSensitiveWords")
public class SmsSensitiveWordsController extends BaseController {

	@Autowired
	private CachRefreshInterface cachRefreshInterface;

	@Autowired
	private SmsSensitiveWordsService smsSensitiveWordsService;
	
	@ModelAttribute
	public SmsSensitiveWords get(@RequestParam(required=false) String id) {
		SmsSensitiveWords entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsSensitiveWordsService.get(id);
		}
		if (entity == null){
			entity = new SmsSensitiveWords();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsSensitiveWords:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsSensitiveWords smsSensitiveWords, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsSensitiveWords> page = smsSensitiveWordsService.findPage(new Page<SmsSensitiveWords>(request, response), smsSensitiveWords); 
		model.addAttribute("page", page);
		return "modules/sms/smsSensitiveWordsList";
	}

	@RequiresPermissions("sms:smsSensitiveWords:view")
	@RequestMapping(value = "form")
	public String form(SmsSensitiveWords smsSensitiveWords, Model model) {
		model.addAttribute("smsSensitiveWords", smsSensitiveWords);
		return "modules/sms/smsSensitiveWordsForm";
	}

	@RequiresPermissions("sms:smsSensitiveWords:edit")
	@RequestMapping(value = "save")
	public String save(SmsSensitiveWords smsSensitiveWords, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsSensitiveWords)){
			return form(smsSensitiveWords, model);
		}
		smsSensitiveWordsService.save(smsSensitiveWords);
		addMessage(redirectAttributes, "保存敏感词成功");
		SensitiveWords sensitiveWords=new SensitiveWords();
		sensitiveWords.setSensitiveWords( smsSensitiveWords.getSensitiveWords() );
		sensitiveWords.setType( smsSensitiveWords.getWordType() );
		cachRefreshInterface.updateSensitiveWord( "1",sensitiveWords );
		return "redirect:"+Global.getAdminPath()+"/sms/smsSensitiveWords/?repage";
	}
	
	@RequiresPermissions("sms:smsSensitiveWords:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsSensitiveWords smsSensitiveWords, RedirectAttributes redirectAttributes) {
		smsSensitiveWordsService.delete(smsSensitiveWords);
		addMessage(redirectAttributes, "删除敏感词成功");
		SensitiveWords sensitiveWords=new SensitiveWords();
		sensitiveWords.setSensitiveWords( smsSensitiveWords.getSensitiveWords() );
		sensitiveWords.setType( smsSensitiveWords.getWordType() );
		cachRefreshInterface.updateSensitiveWord( "2",sensitiveWords );
		return "redirect:"+Global.getAdminPath()+"/sms/smsSensitiveWords/?repage";
	}

}